﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;
using System.IO;

public partial class controls_admin_ServicesEdit : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            try
            {
                loadSetting();
                base.loadModuleByType(lsbModule, 1);
                base.loadCategories(ddlCategories, 5);
                loadData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally { base.CloseConnection(); }
        }
    }
    private void loadData()
    {
        if (Request["s_id"] == null)
        {
            divMessage.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Bản ghi này không tồn tại hoặc đã bị xóa !");
            return;
        }
        DataTable objDT = BO_Services.Search(Convert.ToInt32(Request["s_id"]));
        if (objDT != null && objDT.Rows.Count > 0)
        {
            DataRow row = objDT.Rows[0];
            txtTitle.Text = row["FULL_SERVICE_TITLE"].ToString();
            txtDescription.Text = row["FULL_SERVICE_DESCRIPTION"].ToString();
            txtMetaKeyword.Text = row["META_KEYWORD"].ToString();
            txtMetaDescription.Text = row["META_DESCRIPTION"].ToString();
            ddlCategories.SelectedValue = row["CATEGORY_ID"].ToString();
            cboDisplay.Checked = Convert.ToBoolean(row["IS_DISPLAY_ON_HOME_PAGE"]);
            cboFree.Checked = Convert.ToBoolean(row["IS_FREE"]);
            cboPersonal.Checked = Convert.ToBoolean(row["IS_PERSONAL"]);
            string contain = "-1:";
            if (Request["m_id"] != null)
            {
                DataTable module = BO_Module.Search(Convert.ToInt32(Request["m_id"]));
                string type = module.Rows[0]["TYPE_ID"].ToString();
                if (type.Equals("4"))
                    contain = Request["m_id"].ToString() + ":";
            }
            string sortdisplay = "1";
            string module_id = row["MODULE_ID"].ToString();
            string[] list_module = module_id.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string per_m_id in list_module)
            {
                foreach (ListItem item in lsbModule.Items)
                    if (item.Value.Equals(per_m_id.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[0]))
                        item.Selected = true;
                if (per_m_id.Contains(contain))
                    sortdisplay = per_m_id.Remove(0, contain.Length);
            }
            txtServicesCode.Text = row["SERVICE_CODE"].ToString();
            txtSortDisplay.Text = sortdisplay;
            fkcContent.Value = row["SERVICE_DETAIL"].ToString();
            if (!row["THUMB_IMG"].ToString().Equals(String.Empty))
                divFileName.InnerHtml = "<img src='../upload/services/" + objDT.Rows[0]["THUMB_IMG"].ToString() + "' alt='" + row["FULL_SERVICE_TITLE"].ToString() + "' height='100' />";
            if (!row["FILE_ATTACH_LINK"].ToString().Equals(String.Empty))
                divFileName.InnerHtml = "<font color='red'>" + row["FILE_ATTACH_LINK"].ToString() + "</font>";
        }
        else
            divMessage.InnerHtml = O2S_Message.Warning("Hệ Thống", "Bản ghi này không tồn tại hoặc đã bị xóa.");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            int s_id = Convert.ToInt32(Request["s_id"]);
            DataTable objDT = BO_Services.Search(s_id);
            DataRow row = objDT.Rows[0];
            string path = Server.MapPath("~/upload/services/");
            string filename = row["THUMB_IMG"].ToString();
            string attach = row["FILE_ATTACH_LINK"].ToString();
            try
            {
                if (fulTHUMB_IMG.HasFile)
                {
                    filename = fulTHUMB_IMG.FileName;
                    int count = 0;
                    while (File.Exists(path + filename))
                    {
                        filename = count + "_" + fulTHUMB_IMG.FileName;
                        count++;
                    }
                    fulTHUMB_IMG.SaveAs(path + filename);
                }
                if (fulFileName.HasFile)
                {
                    attach = fulFileName.FileName;
                    int count = 0;
                    while (File.Exists(path + attach))
                    {
                        attach = count + "_" + fulFileName.FileName;
                        count++;
                    }
                    fulFileName.SaveAs(path + attach);
                }
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Lỗi ghi file! " + ex.Message);
                return;
            }
            int c_id = Convert.ToInt32(ddlCategories.SelectedValue);
            int status = 1;
            string title = txtTitle.Text.Trim();
            string description = txtDescription.Text.Trim();
            string details = fkcContent.Value;
            bool displayOnHome = cboDisplay.Checked;
            bool is_personal = cboPersonal.Checked;
            bool is_free = cboFree.Checked;
            DataTable admin = (DataTable)Session["Admin"];
            string sort_display = txtSortDisplay.Text.Trim();
            string module_id = ";-1:" + sort_display + ";";
            string m_key = txtMetaKeyword.Text.Trim();
            string m_desc = txtMetaDescription.Text.Trim();
            string code = txtServicesCode.Text.Trim();
            foreach (ListItem item in lsbModule.Items)
            {
                if (item.Selected)
                    module_id += item.Value + ":" + sort_display + ";";
            }
            string strResult = BO_Services.Update(s_id, c_id, module_id,
                    code, filename, attach, "", "", title, "", "", "", description, "", details, "", m_key, m_desc, 0,
                    0, displayOnHome, is_free, is_personal,
                    DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), DateTime.Now, "", status);
            if (strResult != "0")
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Cập nhật thành công!");
            else
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Có lỗi trong quá trình xử lý!");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    private void loadSetting()
    {
        DataTable objDT = BO_Settings.SelectByGroupId(3, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "NOTE_CATE":
                    NOTE_CATE.InnerText = value;
                    break;
                case "NOTE_DISPLAY":
                    NOTE_DISPLAY.InnerText = value;
                    break;
                case "NOTE_MODULE":
                    NOTE_MODULE.InnerText = value;
                    break;
                case "NOTE_SORT":
                    NOTE_SORT.InnerText = value;
                    break;
                case "NOTE_TITLE":
                    NOTE_TITLE.InnerText = value;
                    break;
                case "NOTE_DESCRIPT":
                    NOTE_DESCRIPT.InnerText = value;
                    break;
                case "NOTE_URL":
                    NOTE_URL.InnerText = value;
                    break;
            }
        }
    }
}
